package com.example.lklibrary.bi.http;

import android.content.Context;
import android.util.Log;

import com.example.lklibrary.bi.bean.AredResponseObj;
import com.example.lklibrary.bi.cache.LKDbManager;
import com.example.lklibrary.bi.config.LKConfig;
import com.example.lklibrary.bi.utils.LKSPUtil;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LkCollectorHttp {

    public static final MediaType JSON
            = MediaType.parse("application/json;charset=utf-8");
    public static OkHttpClient client;
    public static LkCollectorHttp lkCollectorHttp;

    private LkCollectorHttp() {
    }

    public static LkCollectorHttp init() {
        if (null == client) {
            client = new OkHttpClient
                    .Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .build();
        }
        if (null == lkCollectorHttp) {
            lkCollectorHttp = new LkCollectorHttp();
        }
        return lkCollectorHttp;
    }

    /**
     * 获取位置信息
     */
    public void getArea() {

        final Request request = new Request.Builder()
                .url(LKConfig.AREA_GET)
                .get()
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String respons = response.body().string();
                Gson gson = new Gson();
                AredResponseObj post = gson.fromJson(respons, AredResponseObj.class);
                if (null == post) {
                    return;
                }
                if (null == post.data) {
                    return;
                }
                AredResponseObj.DataBeanX.DataBean dataBean = null;
                if (null != post.data.data) {
                    dataBean = post.data.data;
                }
                LKSPUtil.getInstance().putStringValue("area_id", dataBean.area_id);
                LKSPUtil.getInstance().putStringValue("city_id", dataBean.city_id);
                LKSPUtil.getInstance().putStringValue("country_id", dataBean.country_id);
                LKSPUtil.getInstance().putStringValue("region_id", dataBean.region_id);
            }
        });
    }

    /**
     * 上传埋点数据
     */
    public void upCollectInfo(String url, String collector, final Context context, final long time) {

        RequestBody body = RequestBody.create(JSON, collector);
        final Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response){
                LKDbManager.getInstance().deleteUploadSessionData(context, time);
            }
        });
    }

    /**
     * 上传崩溃日志
     */
    public void upExceptionInfo(String url, String exception) {

        RequestBody body = RequestBody.create(JSON, exception);
        final Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //崩溃日志上传成功，删除本地缓存日志
                LKSPUtil.getInstance().putStringValue("errorInfo", "");
            }
        });
    }
}
